package com.situ.company.init;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@WebFilter("/web/page/*")
public class UserFilter implements Filter {

	public void destroy() {
		// TODO Auto-generated method stub

	}
//防盗链，page之后的文件需要登录
	public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
			throws IOException, ServletException {
		// TODO Auto-generated method stub
		HttpServletRequest request = (HttpServletRequest) req;
		HttpSession session = request.getSession();
		Object user = session.getAttribute("user");
		if (user == null)
			request.getRequestDispatcher("/web/login.jsp").forward(request, resp);
		// 跳转页面
		// request.getRequestDispatcher("").forward(request, resp);
		// ((HttpServletResponse)resp).sendRedirect("");
		else
			chain.doFilter(req, resp);

	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub

	}

}
